package struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import struts.bisness.panduanAction;
import struts.from.ActionForm;
import struts.from.FullForm;
import struts.from.XmlBean;

public class ActionServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3137831652369054389L;

	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		
//		Action action = new panduanAction();
//		//获取转发器对象
//		RequestDispatcher dis = request.getRequestDispatcher(action.execute(request));
//		dis.forward(request, response);
		
		String path = this.getPath(request.getServletPath());
		Map<String,XmlBean> map = (Map<String,XmlBean>)this.getServletContext().getAttribute("struts");
		XmlBean xml = map.get(path);
		String formclss = xml.getFormClass();
		ActionForm form = FullForm.full(formclss, request);
		String actionClass = xml.getActionClass();
		Action action = null;
		String url = "";
		try {
			
			Class clazz = Class.forName(actionClass);
			action = (Action)clazz.newInstance();
			url = action.execute(request, form, xml.getActionForward());
		} catch (Exception e) {
			System.out.println("严重：控制器异常");
		}
		RequestDispatcher dis = request.getRequestDispatcher(url);
		dis.forward(request, response);
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		
		this.doGet(request, response);
	}
	
	private String getPath(String servletPath){
		
		return servletPath.split("\\.")[0];
		
	}
	
}
